##
## Comunication functions/commands

package Purl::comm;

no define NONE =>;

### Basic Utility Functions

# sendMessageToContents
# get message, get contents, sent message to it.

# sendMessageToList
# Send a message to a list of dbrefs

sub tellWizards {
    my ($msg) = @_;
    my ($i);
    for ( $i = 0; ( $i <= $#main::activeFds ); $i++ ) {
        my ($e) = $main::activeFds[$i]{"id"};
        if ( $e != NONE ) {
            Purl::comm::sendTextToPlayer( target => $e, text => $msg )
                if ( &Purl::tests::isWizard($e) );
        }
    }
}

# sendMessageToObject takes a dbref and a text string.
# The text is sent to the   of the dbref
sub sendMessageToObject {

    my %args = @_;

    #Filter message. Trailing spaces, ansi character substitution.
    $what =~ s/\s+$//;

    # The gag code should be reworked.. but placed here.

    # When Prefixs for creatures/players/exists are implemented,
    # They are here, for now just send the message to the object's contents.
    _send_text_to_contents( $args{target}, $args{text} );

    # We are done if the object is a room or exit.

    #if its a creature, send it to the owner,
    #    if () {
    # 	   sendTextToPlayer(Purl::database->owner($who),  $what);
    #      return
    #    }
    sendTextToPlayer(%args);
}

# sendTextToPlayer takes a player dbref and a string. it resolves
# The player filehandle from the player attribute and sends the text there.
#
# Server based, non-gagable messages should use this function

sub sendTextToPlayer {

    # Convert player dbref to activeFd.
    my %args = @_;

    #print STDERR "DEBUG: sTtP $args{target}, $args{text}\n";
    if ( $main::objects[ $args{target} ]{"activeFd"} ne NONE ) {

        _send_text_to_ActiveFd( $main::objects[ $args{target} ]{"activeFd"},
            $args{text} );
    }
}

## This sends the text to the activeFD for the player Object.

sub _send_text_to_ActiveFd {
    my ( $active, $what ) = @_;
                                                                     # Test stub.
    #print STDERR "DEBUG: _s_t_t_AFd: $active, $what \n";
    print $active "$what\n";

}

sub getFile {
    my ($fname) = @_;
    my $return;
    if ( !CORE::open( IN, $fname ) ) {
        print STDERR "ERROR: the file " . $fname . " is missing.";
        return;
    }
    while (<IN>) {
        s/\s+$//;
        $return .= $_ . "\n";
    }
    close(IN);

    return $return;
}

sub sendActiveFdFile {
    my ( $i, $fname ) = @_;
    if ( !CORE::open( IN, $fname ) ) {
        _send_text_to_ActiveFd( $i,
            "ERROR: the file " . $fname . " is missing." );
        return;
    }
    while (<IN>) {
        s/\s+$//;
        _send_text_to_ActiveFd( $i, $_ );
    }
    close(IN);
}

sub sendFile {
    my ( $id, $fname ) = @_;
    Purl::comm::sendActiveFdFile( $main::objects[$id]{"activeFd"}, $fname )
        if ( $main::objects[$id]{"activeFd"} ne $none );
}
## Un-sorted functions

sub _send_text_to_contents {

    # rework main::tellroom.
    my ( $id, $blind, $what, $from ) = @_;
    my ( $e, @list, $fromText );
    $fromText = " (from $from)" if ( $from ne "" );
    @list = split( /,/, $main::objects[$id]{"contents"} );
    foreach $e (@list) {
        if (( $main::objects[$e]{"type"} == $main::player )
            || ( $main::objects[$e]{"type"} == $thing
                && ( $main::objects[$e]{"flags"}
                    & $flags{"creature"}{"value"} ) )
            )
        {
            if ( $e != $blind ) {

                # Filter annoyances out (apply gag filters).
                if ( $from ne "" ) {
                    my ($tgag) = quotemeta($from);

                    # The player is not interested.
                    next
                        if (
                        (   $main::objects[ $main::objects[$e]{"owner"} ]
                            {"gags"} =~ /^$tgag /i
                        )
                        || ( $main::objects[ $main::objects[$e]{"owner"} ]
                            {"gags"} =~ / $tgag /i )
                        );
                }
                my ($msg) = $what;
                if ( $main::objects[ $main::objects[$e]{"owner"} ]{"flags"}
                    & $flags{"nospoof"}{"value"} )
                {
                    Purl::comm::sendTextToPlayer(
                        target => $e,
                        text   => $msg . $fromText
                    );
                }
                else {
                    Purl::comm::sendTextToPlayer(
                        target => $e,
                        text   => $msg
                    );
                }
            }
        }
    }
}

1;
